Test Series - Data Structure

Test Number 15/115

Q: What is the best case time complexity of deleting a node in a Singly Linked list?
A. O (n)
B. O (n2)
C. O (nlogn)
D. O (1)
Solution: Deletion of the head node in the linked list is taken as the best case. The successor of the head node is changed to head and deletes the predecessor of the newly assigned head node. This process completes in O(1) time.
Q: Which of the following statements are not correct with respect to Singly Linked List(SLL) and Doubly Linked List(DLL)?
A. Complexity of Insertion and Deletion at known position is O(n) in SLL and O(1) in DLL
B. SLL uses lesser memory per node than DLL
C. DLL has more searching power than SLL
D. Number of node fields in SLL is more than DLL
Solution: To insert and delete at known positions requires complete traversal of the list in worst case in SLL, SLL consists of an item and a node field, while DLL has an item and two node fields, hence SLL occupies lesser memory, DLL can be traversed both ways(left and right), while SLL can traverse in only one direction, hence more searching power of DLL. Node fields in SLL is 2 (data and address of next node) whereas in DLL is 3(data, address to next node, address to previous node).
Q: Given below is the Node class to perform basic list operations and a Stack class with a no arg constructor.
Select from the options the appropriate pop() operation that can be included in the Stack class. Also ‘first’ is the top-of-the-stack.
class Node
{
	protected Node next;
	protected Object ele;
	Node()
	{
		this(null,null);
	}
	Node(Object e,Node n)
	{
		ele=e;
		next=n;
	}
	public void setNext(Node n)
	{
		next=n;
	}
	public void setEle(Object e)
	{
		ele=e;
	}
	public Node getNext()
	{
		return next;
	}
	public Object getEle()
	{
		return ele;
	}
}
 
class Stack
{
	Node first;
	int size=0;
	Stack()
	{
		first=null;
	}
}
A. public Object pop() { if(size == 0) System.out.println("underflow"); else { Object o = first.getEle(); first = first.getNext(); size--; return o; } }
B. public Object pop() { if(size == 0) System.out.println("underflow"); else { Object o = first.getEle(); first = first.getNext().getNext(); size--; return o; } }
C. public Object pop() { if(size == 0) System.out.println("underflow"); else { first = first.getNext(); Object o = first.getEle(); size--; return o; } }
D. public Object pop() { if(size == 0) System.out.println("underflow"); else { first = first.getNext().getNext(); Object o = first.getEle(); size--; return o; } }
Solution: pop() should return the Object pointed to by the node ‘first’. The sequence of operations is, first, get the element stored at node ‘first’ using getEle(), and second, make the node point to the next node using getNext().
Q: What does the following function do?

public Object some_func()throws emptyStackException
{
	if(isEmpty())
		throw new emptyStackException("underflow");
	return first.getEle();
}
A. pop
B. delete the top-of-the-stack element
C. retrieve the top-of-the-stack element
D. push operation
Solution: This code is only retrieving the top element, note that it is not equivalent to pop operation as you are not setting the ‘next’ pointer point to the next node in sequence.
Q: What is the functionality of the following piece of code?

public void display() 
{
	if(size == 0)
		System.out.println("underflow");
	else
	{
		Node current = first;
		while(current != null)
		{
			System.out.println(current.getEle());
			current = current.getNext();
		}
	}
}
A. reverse the list
B. display the list
C. display the list excluding top-of-the-stack-element
D. reverse the list excluding top-of-the-stack-element
Solution: An alias of the node ‘first’ is created which traverses through the list and displays the elements.
Q: What does ‘stack overflow’ refer to?
A. accessing item from an undefined stack
B. adding items to a full stack
C. removing items from an empty stack
D. index out of bounds exception
Solution: Adding items to a full stack is termed as stack underflow.
Q: Given below is the Node class to perform basic list operations and a Stack class with a no arg constructor. Select from the options the appropriate push() operation that can be included in the Stack class. Also ‘first’ is the top-of-the-stack.

class Node
{
	protected Node next;
	protected Object ele;
	Node()
	{
		this(null,null);
	}
	Node(Object e,Node n)
	{
		ele=e;
		next=n;
	}
	public void setNext(Node n)
	{
		next=n;
	}
	public void setEle(Object e)
	{
		ele=e;
	}
	public Node getNext()
	{
		return next;
	}
	public Object getEle()
	{
		return ele;
	}
}
 
class Stack
{
	Node first;
	int size=0;
	Stack()
	{
		first=null;
	}
}
A. public void push(Object item) { Node temp = new Node(item,first); first = temp; size++; }
B. public void push(Object item) { Node temp = new Node(item,first); first = temp.getNext(); size++; }
C. public void push(Object item) { Node temp = new Node(); first = temp.getNext(); first.setItem(item); size++; }
D. public void push(Object item) { Node temp = new Node(); first = temp.getNext.getNext(); first.setItem(item); size++; }
Solution:  To push an element into the stack, first create a new node with the next pointer point to the current top-of-the-stack node, then make this node as top-of-the-stack by assigning it to ‘first’.
Q: Consider these functions:
push() : push an element into the stack
pop() : pop the top-of-the-stack element
top() : returns the item stored in top-of-the-stack-node
What will be the output after performing these sequence of operations

push(20);
push(4);
top();
pop();
pop();
pop();
push(5);
top();
A. 20
B.  4
C. stack underflow
D. 5
Solution: 20 and 4 which were pushed are popped by the two pop() statements, the recent push() is 5, hence top() returns 5.
Q: Which of the following data structures can be used for parentheses matching?
A. n-ary tree
B. queue
C. priority queue
D. stack
Solution: For every opening brace, push it into the stack, and for every closing brace, pop it off the stack. Do not take action for any other character. In the end, if the stack is empty, then the input has balanced parentheses.
Q: Minimum number of queues to implement stack is ___________
A. 3
B. 4
C. 1
D. 2
Solution: Use one queue and one counter to count the number of elements in the queue.

You Have Score    /10